using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ScrollRectSnapElements : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IEventSystemHandler
{
	protected ScrollRect m_Rect;

	protected bool m_Dragging;

	protected RectTransform m_RectContent;

	protected bool m_MoveTo;

	[SerializeField]
	protected float m_SpeedThreshold;

	[SerializeField]
	[Range(0.01f, 0.2f)]
	protected float m_SnapTime;

	protected float m_Gap;

	private void Awake()
	{
		m_Rect = GetComponent<ScrollRect>();
		m_RectContent = m_Rect.content;
		GridLayoutGroup component;
		if ((bool)(component = m_RectContent.GetComponent<GridLayoutGroup>()))
		{
			Vector2 cellSize = component.cellSize;
			float x = cellSize.x;
			Vector2 spacing = component.spacing;
			m_Gap = x + spacing.x;
		}
		else
		{
			UnityEngine.Debug.LogError("No grid layout group component attach at the scroll rect content !");
		}
	}

	private void Update()
	{
		if (!m_Dragging)
		{
			Vector2 velocity = m_Rect.velocity;
			if (Mathf.Abs(velocity.x) <= m_SpeedThreshold && m_MoveTo)
			{
				m_MoveTo = false;
				Vector2 anchoredPosition = m_RectContent.anchoredPosition;
				float num = -MathTools.Round2Number(Mathf.Abs(anchoredPosition.x), (int)m_Gap);
				m_Rect.velocity = Vector2.zero;
				GameObject gameObject = m_RectContent.gameObject;
				object[] obj = new object[12]
				{
					"from",
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null
				};
				Vector2 anchoredPosition2 = m_RectContent.anchoredPosition;
				obj[1] = anchoredPosition2.x;
				obj[2] = "to";
				obj[3] = num;
				obj[4] = "time";
				obj[5] = m_SnapTime;
				obj[6] = "onupdate";
				obj[7] = "MotionBlur";
				obj[8] = "onupdatetarget";
				obj[9] = base.gameObject;
				obj[10] = "easetype";
				obj[11] = iTween.EaseType.easeOutCubic;
				iTween.ValueTo(gameObject, iTween.Hash(obj));
			}
		}
	}

	private void MotionBlur(float value)
	{
		RectTransform rectContent = m_RectContent;
		Vector2 anchoredPosition = m_RectContent.anchoredPosition;
		rectContent.anchoredPosition = new Vector2(value, anchoredPosition.y);
	}

	public void OnEndDrag(PointerEventData eventData)
	{
		m_Dragging = false;
	}

	public void OnBeginDrag(PointerEventData eventData)
	{
		m_Dragging = true;
		m_MoveTo = true;
	}
}
